package com.tianquan.miracle.autoConfigurationBean;/**
 * @author starsea
 * @date 2024-10-07 上午10:05
 */

import com.tianquan.miracle.annotation.Configuration;
import com.tianquan.miracle.conditional.ConditionalOnMissingBean;
import com.tianquan.miracle.util.MiracleUtils;
import jodd.util.StringUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.Properties;

/**
 * @BelongsProject: uploaddemo
 * @BelongsPackage: com.tianquan.miracle.autoConfigurationBean
 * @Author: tianquan
 * @CreateTime: 2024-10-07  10:05
 * @Description: redisson自动配置类
 * @Version: 1.0
 */

@Configuration
public class RedissionAutoConfiguration {

    private String localHost;

    private String port;

    public RedissionAutoConfiguration() {
        Properties properties = MiracleUtils.getProperties();
        this.localHost = properties.getProperty("redis.localhost");
        this.port = properties.getProperty("redis.port");
    }

    @ConditionalOnMissingBean
    public RedissonClient redissonClient() {
        if (StringUtil.isNotEmpty(localHost) && StringUtil.isNotEmpty(port)) {
            Config config = new Config();
            config.useSingleServer().setAddress("redis://" + localHost + ":" + port);
            return Redisson.create(config);
        }
        return null;
    }

}
